<?php

namespace App\Http\Controllers\Admin;

use App\Models\Metro;
use App\Services\Admin\MetroService;
use App\Services\Admin\RegionService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class MetroController extends Controller
{
    protected $service;
    protected $regionService;

    public function __construct(RegionService $regionService, MetroService $metroService)
    {
        $this->regionService = $regionService;
        $this->service = $metroService;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // 区域
        $area = $this->regionService->getArea(0);
        return view('admin.metro.index',compact('area'));
    }

    public function data(Request $request)
    {
        $model = Metro::query();
        if ($request->get('province_id')){
            $model = $model->where('province_id',$request->get('province_id'));
        }
        if ($request->get('region_id')){
            $model = $model->where('region_id',$request->get('region_id'));
        }
        if ($request->get('name')){
            $model = $model->where('name','like','%'.$request->get('name').'%');
        }
        $res = $model->orderBy('sort','asc')->paginate($request->get('limit',30))->toArray();
        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' => $res['total'],
            'data'  => $res['data']
        ];
        return response()->json($data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //区域
        $area = $this->regionService->getArea(0);

        return view('admin.metro.create',compact('area'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request,[
            'name'  => 'required|string',
            'region'  => 'required'
        ]);
        $data = $request->all();
        $pro = explode(',',$data['province']);
        $data['province_id'] = $pro[0];
        $data['province_name'] = $pro[1];
        $reg = explode(',',$data['region']);
        $data['region_id'] = $reg[0];
        $data['region'] = $reg[1];
        $metro = Metro::create($data);
        if ($metro){
            return redirect(route('admin.metro'))->with(['status'=>'添加成功']);
        }
        return redirect(route('admin.metro'))->withErrors(['status'=>'系统错误']);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //区域
        $area = $this->regionService->getArea(0);
        $metro = Metro::findOrFail($id);
        if (!$metro){
            return redirect(route('admin.metro'))->withErrors(['status'=>'地铁线不存在']);
        }
        return view('admin.metro.edit',compact('metro','area'));

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request,[
            'name'  => 'required|string',
            'region'  => 'required'
        ]);
        $metro = Metro::findOrFail($id);
        $data = $request->all();
        $pro = explode(',',$data['province']);
        $data['province_id'] = $pro[0];
        $data['province_name'] = $pro[1];
        $reg = explode(',',$data['region']);
        $data['region_id'] = $reg[0];
        $data['region'] = $reg[1];

        if ($metro->update($data)){
            return redirect(route('admin.metro'))->with(['status'=>'更新成功']);
        }
        return redirect(route('admin.metro'))->withErrors(['status'=>'系统错误']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        $ids = $request->get('ids');
        if (empty($ids)){
            return response()->json(['code'=>1,'msg'=>'请选择删除项']);
        }
        foreach (Metro::whereIn('id',$ids)->get() as $model){
            //删除
            $model->delete();
        }
        return response()->json(['code'=>0,'msg'=>'删除成功']);
    }

    public function getMetroByCity(Request $request)
    {
        return $this->service->getMetroByCity($request->input('city_id'));
    }
}
